package com.fmsz.physicalheadline.activitys.news;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.fmsz.physicalheadline.BR;
import com.fmsz.physicalheadline.R;
import com.fmsz.physicalheadline.adapter.news.RecycleAdapter;
import com.fmsz.physicalheadline.application.MyApplication;
import com.fmsz.physicalheadline.databinding.ActivityFocusImageBinding;
import com.fmsz.physicalheadline.entity.news.NewsContents;
import com.fmsz.physicalheadline.entity.news.focus.ChildEntity;
import com.fmsz.physicalheadline.entity.news.focus.FocusDataEntity;
import com.fmsz.physicalheadline.entity.news.focus.FocusEntity;
import com.fmsz.physicalheadline.interfaces.HttpService;
import java.util.ArrayList;
import java.util.HashMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FocusImageActivity extends AppCompatActivity implements Callback<FocusEntity> {

    private Long link;
    private ActivityFocusImageBinding binding;
    private RecycleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_focus_image);
        Intent intent = getIntent();
        link = intent.getLongExtra("link",-1);
        if (link==-1) {
            Toast.makeText(this, "抱歉,页面不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        HttpService httpService = ((MyApplication) getApplication()).getHttpService();
        httpService.getFocusActivityData(link).enqueue(this);
        HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(FocusDataEntity.class,Pair.create(R.layout.contest_title_item,BR.contest_title));
        map.put(ChildEntity.class,Pair.create(R.layout.contest_content_item,BR.contest_content));
        map.put(NewsContents.class,Pair.create(R.layout.news_normal,BR.news_content));
        adapter = new RecycleAdapter<>(this,new ArrayList<>(),map);
        binding.recycleFocus.setAdapter(adapter);
    }

    @Override
    public void onResponse(Call<FocusEntity> call, Response<FocusEntity> response) {
        if (response.body()!=null) {
            if (response.body().getCode() != 200) {
                Toast.makeText(this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                return;
            }
            binding.setVariable(BR.focus, response.body().getData());
            adapter.add(response.body().getData());
            for (int i = 0; i < response.body().getData().getChilds().size(); i++) {
                adapter.add(response.body().getData().getChilds().get(i));
                adapter.addAll(response.body().getData().getChilds().get(i).getContents());
            }
        }
    }

    @Override
    public void onFailure(Call<FocusEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
